package com.ruoyi.common.utils;


import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.github.binarywang.wxpay.v3.util.AesUtils;
import com.ruoyi.common.utils.sign.Base64;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.engines.SM4Engine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import sun.security.krb5.internal.crypto.Aes256;

import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

public class SM4Util {

    private static final String fixedKey = "386B3D423300D3592B1CE86A11170EFF";

    public static String sm4EncryptUtil(String params){

        byte[] key = hexStr2Bytes(fixedKey);

        SymmetricCrypto sm4 = SmUtil.sm4(key);

        return Base64.encode(sm4.encrypt(params));
    }

    public static byte[] hexStr2Bytes(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length()/2];
        int n;
        for(int i = 0; i < bytes.length; i++){
            n = str.indexOf(hexs[2*i])*16;
            n += str.indexOf(hexs[2*i+1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return bytes;
    }
}
